package edu.ncsu.csc.streamingmr;

import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;

public class FixedLengthText {
	private final static int FIXED_LENGTH=32;
	private final static byte[] blank;
	
	static{
		blank=new byte[FIXED_LENGTH];
		for(int i=0; i<FIXED_LENGTH;i++)
			blank[i]=' ';
	}
	private ByteBuffer textbuf=ByteBuffer.allocate(FIXED_LENGTH); 
	
	public static int getLength() {
		return FIXED_LENGTH;
	}
	
	public void set(byte[] text) throws BufferOverflowException{
		if(text.length>FIXED_LENGTH)
			throw new BufferOverflowException();
		textbuf.clear();
		textbuf.put(text);
	}
	
	public byte[] getText() {
		return textbuf.array();
	}
	
	public void set(byte[] text, int offset, int length) throws BufferOverflowException{
		if(length>FIXED_LENGTH)
			throw new BufferOverflowException();
		textbuf.clear();
		textbuf.put(blank);
		textbuf.clear();
		textbuf.put(text, offset, length);
	}
}
